﻿using System;
using System.ComponentModel;

using DevExpress.Xpo;
using DevExpress.Data.Filtering;

using DevExpress.ExpressApp;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;
using DevExpress.Persistent.Base.Security;
using System.Collections.Generic;
using System.Security;

namespace SpeedSCM.Module
{
    [NavigationItem("基本信息")]
    [ImageName("BO_User")]
    [DefaultProperty("FullName")]
    public class 职员 : BaseObject, ISimpleUser, IUserWithRoles, IAuthenticationActiveDirectoryUser, IAuthenticationStandardUser
    {
        private List<IPermission> permissions;
        private UserImpl userImpl;

        public 职员(Session session)
            : base(session)
        {
            permissions = new List<IPermission>();
        }
        public bool ComparePassword(string password)
        {
            return this.user.ComparePassword(password);
        }

        public void SetPassword(string password)
        {
            this.user.SetPassword(password);
        }

        [RuleRequiredField(null,DefaultContexts.Save, "用户名不能为空")]
        [RuleUniqueValue(null, DefaultContexts.Save,"用户名已经存在")]
        [Custom("Caption", "用户名")]
        public string UserName
        {
            get
            {
                return this.user.UserName;
            }
            set
            {
                this.user.UserName = value;
                base.OnChanged("UserName");
            }
        }
        [Custom("Caption","姓名")]
        [RuleRequiredField(null,DefaultContexts.Save,"姓名不能为空")]
        public string FullName
        {
            get
            {
                return this.user.FullName;
            }
            set
            {
                this.user.FullName = value;
                base.OnChanged("FullName");
            }
        }
        [Custom("Caption","首次登录之前必须更改密码")]
        public bool ChangePasswordOnFirstLogon
        {
            get
            {
                return this.user.ChangePasswordAfterLogon;
            }
            set
            {
                this.user.ChangePasswordAfterLogon = value;
                base.OnChanged("ChangePasswordAfterLogon");
            }
        }
        
        [Custom("Caption","账户是否已激活")]
        public bool IsActive
        {
            get
            {
                return this.user.IsActive;
            }
            set
            {
                this.user.IsActive = value;
                base.OnChanged("IsActive");
            }
        }
        [Custom("Caption","是否是管理员")]
        public bool IsAdministrator
        {
            get
            {
                return this.user.IsAdministrator;
            }
            set
            {
                this.user.IsAdministrator = value;
                base.OnChanged("IsAdministrator");
            }
        }

        [Persistent]
        protected string Password
        {
            get
            {
                return this.user.StoredPassword;
            }
            set
            {
                this.user.StoredPassword = value;
                base.OnChanged("StoredPassword");
            }
        }

        private UserImpl user
        {
            get
            {
                if (this.userImpl == null)
                {
                    this.userImpl = new UserImpl(this);
                }
                return this.userImpl;
            }
        }
        

        private string _职员编号;
        [Custom("Caption", "职员编号")]
        public string 职员编号
        {
            get { return _职员编号; }
            set { SetPropertyValue<string>("职员编号", ref _职员编号, value); }
        }
        private string _所在部门;
        [Custom("Caption", "所在部门")]
        public string 所在部门
        {
            get { return _所在部门; }
            set { SetPropertyValue<string>("所在部门", ref _所在部门, value); }
        }
        private string _电话号码;
        [Custom("Caption", "电话号码")]
        public string 电话号码
        {
            get { return _电话号码; }
            set { SetPropertyValue<string>("电话号码", ref _电话号码, value); }
        }
        private string _联系人;
        [Custom("Caption", "联系人")]
        public string 联系人
        {
            get { return _联系人; }
            set { SetPropertyValue<string>("联系人", ref _联系人, value); }
        }
        private string _备注;
        [Custom("Caption", "备注")]
        public string 备注
        {
            get { return _备注; }
            set { SetPropertyValue<string>("备注", ref _备注, value); }
        }
        [Association("职员-角色")]
        [RuleRequiredField(null, DefaultContexts.Save, TargetCriteria = "IsActive=True", CustomMessageTemplate = "激活的账户必须至少有一个角色")]
        public XPCollection<角色> 角色
        {
            get { return GetCollection<角色>("角色"); }
        }
        IList<IRole> IUserWithRoles.Roles
        {
            get { return new ListConverter<IRole, 角色>(角色); }
        }

        IList<IPermission> IUser.Permissions
        {
            get
            {
                permissions.Clear();
                foreach (角色 role in 角色)
                {
                    permissions.AddRange(role.Permissions);
                }
                return permissions.AsReadOnly();
            }
        }

        void IUser.ReloadPermissions()
        {
            角色.Reload();
            foreach (角色 role in 角色)
            {
                role.PersistentPermissions.Reload();
            }
        }

    }

 
}
